<?php
/**
 * Author:zhibin@leju.sina.com.cn
 * Date:2010-04-07
 * DESC:上传图片
 *
 */
class T_Imageupload
{
	
	public $permission = array(
						'image/jpeg'=>"jpg", 
						'image/gif'=>"gif",
						'image/png'=>"png"
						);
	
	public $maxsize = 1000000;
	
	public $size = 0;
	
	public $extend = '';
	
	public $oldname = '';
	
	public $newname = '';
	
	public $file = '';
	
	public $path = '';
	
	const  NOIMAGE = -1;  //图片不存在
	
	const  NOSIZE  = -2;  // 图片尺寸不对
	
	const  NOEXTEND = -3;  //图片类型不对
	
	const  NOUPLOAD = -4;  //上传失败
	
	const  NOPATH = -5;  //保存路近不存在
	const  NOWH  = -6;
	const  SUCCESS = 1;  
	
	public function check($file, $wh)
	{
	    if(!is_array($file) || !is_file($file['tmp_name']) || $file['error'] || !is_uploaded_file($file['tmp_name']))
        {
            return self::NOIMAGE;
        }
	    $size = $this->get_size($size);
	    
        if($size > $this->maxsize)
        {
            return self::NOSIZE;
        }
        $extend = $this->get_extend($file);
      	
        if(empty($extend))
        {
            return self::NOEXTEND;
        }
        if(!empty($wh))
        {
	        $type=getimagesize($file['tmp_name']);
			$width= $type[0];
			$height = $type[1];
	        if($width < $wh[0] || $height < $wh[1])
	        {
	        	return self::NOWH;
	        }
        }
        return self::SUCCESS;
	}
	public function get_size($file)
	{
		$size = filesize($file['tmp_name']);
		return $size;
	}
	public function get_extend($file)
	{
		$type=getimagesize($file['tmp_name']);
		$img_type=$type[mime];
		$end = $this->permission[$img_type];	
	    return $end;
	}
	public function save($file , $savepath , $name = '', $wh=array())
	{
		$status = $this->check($file ,$wh);
		if($status != self::SUCCESS)
		{
			return  array('error'=>$status);
		}
       
        $tmp = $file['tmp_name'];
        
        $extend = $this->get_extend($file);
        $size = $this->get_size($file);
        
        if(empty($name))
        {
        	$filename = strtolower(md5($tmp.time())).'.'.$extend;
        }
        else
        {
        	$filename = $name.'.'.$extend;
        }
	    $this->directory($savepath);
        $filepath = $savepath.'/'.$filename;
        
       
        if(!move_uploaded_file($tmp, $filepath))
        {
        	return  array('error'=>self::NOUPLOAD);
        }
        $fileinfo = array(  'error'=>self::SUCCESS, 
					        'oldname'=>$file['name'], 
					        'newname'=>$filename, 
					        'size'=>$size, 
					        'extend'=>$extend,
        					'path' => $savepath,
      					  );
        
        $this->file = $file;
        $this->extend = $extend;
        $this->oldname = $file['name'];
        $this->newname = $filename;
        $this->size = $size;
        $this->path = $savepath;
        return $fileinfo;
	}
	private function directory($dir)
	{    
    	return is_dir($dir) or ($this->directory(dirname($dir)) and mkdir($dir, 0777));
	}

}
?>